package org.testis.gui.mvc.model.dynamicmap;

import org.testis.orm.refinedclasses.DynamicMapParameter;
import org.testis.utils.parametermodel.ParameterModel;

import org.testis.gui.mvc.model.*;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapController;
import org.testis.gui.mvc.utilities.ParameterTableModel;


/**
 *
 * @author Cahit Emir Erel
 */
public class DynamicMapModel extends AbstractModel
{

    private Boolean m_dynamicMapVisibility = false;
    private ParameterTableModel m_model = null;
    private String m_groupType = "";
    private String m_name = "";
    private String m_parameter = "";
    private String m_type = "";
    private String m_value = "";


    public DynamicMapModel()
    {
        super();
    }


    public DynamicMapModel(ParameterTableModel model)
    {
        super();
        setModel(model);
    }


    public String getName()
    {
        return m_name;
    }


    public void setName(String name)
    {
        if (name == null) {
            name = "";
        }
        String oldName = m_name;
        m_name = name;
        firePropertyChange(DynamicMapController.CHANGE_NAME,
                           oldName,
                           name);
    }


    public void setDynamicMapVisibility(Boolean dynamicMapVisibility)
    {
        if (dynamicMapVisibility == null) {
            return;
        }
        Boolean oldDynamicMapVisibility = m_dynamicMapVisibility;
        m_dynamicMapVisibility = dynamicMapVisibility;
        firePropertyChange(DynamicMapController.SHOW_ADD_DYNAMIC_MAP,
                           oldDynamicMapVisibility,
                           dynamicMapVisibility);
    }


    public Boolean getDynamicMapVisibility()
    {
        return m_dynamicMapVisibility;
    }


    public String getGroupType()
    {
        return m_groupType;
    }


    public void setGroupType(String groupType)
    {
        if (groupType == null) {
            groupType = "";
        }
        String oldGroupType = m_groupType;
        m_groupType = groupType;
        firePropertyChange(DynamicMapController.CHANGE_GROUP_TYPE,
                           oldGroupType,
                           groupType);
    }


    public String getParameter()
    {
        return m_parameter;
    }


    public void setParameter(String parameter)
    {
        if (parameter == null) {
            parameter = "";
        }
        String oldParameter = m_parameter;
        m_parameter = parameter;
        firePropertyChange(DynamicMapController.CHANGE_PARAMETER,
                           oldParameter,
                           parameter);
    }


    public String getValue()
    {
        return m_value;
    }


    public void setValue(String value)
    {
        if (value == null) {
            value = "";
        }
        String oldValue = m_value;
        m_value = value;
        firePropertyChange(DynamicMapController.CHANGE_VALUE,
                           oldValue,
                           value);
    }


    public void setAddParameterToList(Boolean add)
    {
        if (add != null && add && !m_parameter.equals("")) {
            for (int i = 0; i < getModel().getRowCount(); i++) {
                if (getModel().getItem(i).getParameter().equals(m_parameter)) {
                    getModel().removeItem(i);
                    break;
                }
            }
            ParameterModel temp = new ParameterModel(m_parameter,
                                                     m_value);
            getModel().addItem(temp);
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               null,
                               getModel());
            if (m_parameter != null && m_parameter.equals("Id")) {
                this.setName(m_value);
            }
        }
    }


    public void setAddParameterToList(String parameter)
    {
        if (parameter != null && !parameter.equals("")) {
            ParameterModel temp = new ParameterModel(parameter,
                                                     "");
            getModel().addItem(temp);
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               null,
                               getModel());
//            if (parameter.equals("Id")) {
//                this.setName("");
//            }
        }
    }


    public void setAddParameterToList(DynamicMapParameter parameter)
    {
        if (parameter != null) {
            for (int i = 0; i < getModel().getRowCount(); i++) {
                if (getModel().getItem(i).getParameter().equals(parameter.getName())) {
                    getModel().removeItem(i);
                    break;
                }
            }
            ParameterModel temp = new ParameterModel(parameter.getName(),
                                                     parameter.getReference().
                                                     getValue());
            getModel().addItem(temp);
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               null,
                               getModel());
            if (parameter != null && parameter.getName() != null && parameter.getName().
                equals("Id")) {
                this.setName(parameter.getReference().getValue());
            }
        }
    }


    public void setAddParameterToList(ParameterModel parameter)
    {
        if (parameter != null) {
            for (int i = 0; i < getModel().getRowCount(); i++) {
                if (getModel().getItem(i).getParameter().equals(parameter.getParameter())) {
                    getModel().removeItem(i);
                    break;
                }
            }
            getModel().addItem(parameter);
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               null,
                               getModel());
            if (parameter != null && parameter.getParameter() != null && parameter.getParameter().
                equals("Id")) {
                this.setName(parameter.getValue());
            }
        }
    }


    public void setAddParameterToList(ParameterModel[] parameter)
    {
        if (parameter != null && parameter.length > 0) {
            for (int j = 0; j < parameter.length; j++) {
                getModel().addItem(parameter[j]);
                if (parameter[j] != null && parameter[j].getParameter() != null && parameter[j].getParameter().
                    equals("Id")) {
                    this.setName(parameter[j].getValue());
                }
            }
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               null,
                               getModel());
        }
    }


    public void setRemoveSelectedParameterFromList(Integer selectedParameter)
    {
        if (selectedParameter != null && selectedParameter < getModel().
            getRowCount() && selectedParameter >= 0) {
            getModel().removeItem(selectedParameter);
            firePropertyChange(DynamicMapController.REMOVE_SELECTED_PARAMETER,
                               null,
                               getModel());
        }
    }


    public void setRemoveAllParametersFromList(Boolean remove)
    {
        if (remove != null && remove) {
            getModel().removeAllItems();
            firePropertyChange(DynamicMapController.REMOVE_ALL_PARAMETERS,
                               null,
                               getModel());
        }
    }


    public String getType()
    {
        return m_type;
    }


    public void setType(String type)
    {
        if (type == null) {
            type = "";
        }
        String oldType = m_type;
        m_type = type;
        firePropertyChange(DynamicMapController.CHANGE_TYPE,
                           oldType,
                           type);
    }


    public ParameterTableModel getModel()
    {
        if (m_model == null) {
            m_model = new ParameterTableModel();
        }
        return m_model;
    }


    public void setModel(ParameterTableModel model)
    {
        if (model != null) {
            ParameterTableModel oldModel = m_model;
            m_model = model;
            firePropertyChange(DynamicMapController.ADD_PARAMETER,
                               oldModel,
                               getModel());
        }
    }
}
